﻿// #include <vld.h>
#include <QApplication>
#include <QScreen>
#include <QDir>

// #include "ElaApplication.h"
#include "mainwindow.h"
#include "BaseModule/GlobalHelper.h"
#include "BaseModule/AppManager.h"
#include "ScriptManager/initModule.h"
#include "CommandManager/initModule.h"

int main(int argc, char* argv[])
{

    // 高分辨率屏幕缩放适配
#if (QT_VERSION >= QT_VERSION_CHECK(5, 6, 0))
    {
        QGuiApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
        QGuiApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
        QGuiApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
    }
#endif

    QApplication _app(argc, argv);

    pst::MainWindow _window;

    AppManagerInstance->init(&_window);
    auto defaultDir = QApplication::applicationDirPath() + "/../examples";
    _window.setInputDirList({defaultDir});
    auto defaultOutputDir = QApplication::applicationDirPath() + "/../SubmitEvaluation";
    QDir _dir(defaultOutputDir);
    if (!_dir.exists(defaultOutputDir))
    {
        qDebug() << "Warning: Output path does not exist";
        auto isOk = _dir.mkpath(defaultOutputDir);
        isOk ? qDebug() << "Output path creation succeeded!" : qDebug() << "Warning: Output path creation failed!";
    }
    _window.setOutputDir(defaultOutputDir);

    ScriptManager::initModule();
    CommandManager::initModule();

    QList<QString> inputDirs;
    QString outputDir;

    if (argc < 2)
    {
        _window.setIsNotInput(true);
    }
    else if (argc == 2)
    {
        auto firstArg = QString::fromStdWString(GlobalHelperInstance->stringToWString(argv[1]));
        inputDirs.append(firstArg);
        _window.setInputDirList(inputDirs);
    }
    else if (argc > 2)
    {
        auto firstArg = QString::fromStdWString(GlobalHelperInstance->stringToWString(argv[1]));
        auto secondArg = QString::fromStdWString(GlobalHelperInstance->stringToWString(argv[2]));
        inputDirs.append(firstArg);
        outputDir = secondArg;
        _window.setInputDirList(inputDirs);
        _window.setOutputDir(outputDir);
    }

    qDebug() << "inputDir = " << inputDirs;
    qDebug() << "outputDir = " << outputDir;

    _window.show();

    int _exitState = _app.exec();

    ScriptManager::cleanupModule();
    CommandManager::cleanupModule();
    qDebug()<<"program exitState = "<<_exitState;
    return _exitState;
}
